﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using System.Collections.Generic;
using System.Linq;
using Teleware.Newbe.Navigation.Web.Domain.Entities;

namespace Teleware.Newbe.Navigation.Web.AppServices.Impl
{
    internal class DepartmentService : ApplicationService, IDepartmentService
    {
        private readonly IRepository<Department, string> _departmentsRepository;
        private readonly IObjectMapper _objectMapper;

        public DepartmentService(IRepository<Department, string> departmentsRepository
            , IObjectMapper objectMapper)
        {
            _departmentsRepository = departmentsRepository;
            _objectMapper = objectMapper;
        }

        public ListResultDto<SimpleDepartmentDto> GetAll()
        {
            var departments =
                _objectMapper.Map<SimpleDepartmentDto[]>(_departmentsRepository.GetAllList().OrderBy(x => x.OrderNo));
            var re = new ListResultDto<SimpleDepartmentDto>
            {
                Items = new List<SimpleDepartmentDto>(departments)
            };
            return re;
        }

        public void Remove(RemoveDepartmentIput input)
        {
            _departmentsRepository.Delete(input.Id);
        }

        public DepartmentDto Get(EntityDto<string> input)
        {
            var department = _departmentsRepository.Get(input.Id);
            if (department == null)
            {
                return null;
            }
            var re = _objectMapper.Map<DepartmentDto>(department);
            return re;
        }
    }

}
